<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



require_once(APPPATH .'/libraries/REST_Controller.php');



/**
 * class Stem
 * Will be the main controller for the vta application.
 *
 * @author      Jeroen Sen <jeroensen@22vision.nl>
 * @version     20120722
 *
 */
class Stem extends REST_Controller {



    /**
     * public function stem
     * curl -H "Accept: application/json" http://zichtopbomen.local/vta/stem/all
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120722
     * @param       
     * @return      void
     *
     */
    public function all_get()
    {
        //$this->load->view('test_message');
        
        /*
echo BASEPATH;
        echo '<br/>';
        echo APPPATH;
*/

/*         $this->response('Werkt dit, vriendelijke vriend?'); */


/*         $stem = $this->doctrine->em->find('Entities\Stem', 1); */

/*         $stems = $this->doctrine->em->getRepository('Entities\Stem')->findAll(); */

        $criteria = array();
        $orderBy = array('name' => 'ASC');

        $stems = $this->doctrine->em->getRepository('Entities\Stem')->findBy($criteria, $orderBy);
        
/*         var_dump($stems); */
        
        $data = array();
        
        foreach ($stems as $index => $stem) {
            $data[] = array('id' => $stem->getId(), 'name' => $stem->getName(), 'value' => $stem->getValue(), 'active' => $stem->getActive());
        }
        
        $this->response(array('data' => $data));
        
    } // public function stem($action = null)
    
    
    
    /**
     * public function create_put
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120731
     *
     */
    public function create_put()
    {
        $active = false;
        
        if ($this->put('active') == 'true') {
            $active = true;
        }
        
        $stem = new Entities\Stem;
        
        $stem->setName($this->put('name'));
        $stem->setValue($this->put('value'));
        $stem->setActive($active);
        
        $this->doctrine->em->persist($stem);
		$this->doctrine->em->flush();

        $this->response('true');
           
    } // public function create_put()
    
    
    
    /**
     * public function update_post
     * curl -X POST -H "Accept: application/json" -d '{"id":666}' http://zichtopbomen.local/vta/stem/edit
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120731
     *
     */
    public function update_post()
    {
        $id = intval($this->post('id'));
        $active = false;
        
        if ($this->post('active') == 'true') {
            $active = true;
        }
        
        
        $stem = $this->doctrine->em->find('Entities\Stem', $id);
                
        $stem->setName($this->post('name'));
        $stem->setValue($this->post('value'));
        $stem->setActive($active);
        
        $this->doctrine->em->persist($stem);
		$this->doctrine->em->flush();

        $this->response('true');
           
    } // public function update_post()
 
    
    
    /**
     *
     *
     */
    public function test_get()
    {
        $this->response(array('data' => 'dit is een test'));
        
    } //public function test_get()
    
    
    
    
} // class Vta extends CI_Controller